/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilities;

import GUI.MainFunction;
import GUI.OptionMessage;
import GUI.ViewStaffForm;
import connection.ManLabConnection;
import entities.StaffDetail;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author VietLH
 */
public class StaffManage {

    private ArrayList<StaffDetail> staffList;
    private ManLabConnection cnn;
    private ResultSet rs;
    private String qry;
    private MainFunction main;
    private OptionMessage optionMessage;
    private SimpleDateFormatter fDate;
    private ValidateInput vi;
    private ArrayList<String> query;

    public StaffManage(ArrayList<StaffDetail> staffList, MainFunction main) {
        this.staffList = new ArrayList<StaffDetail>();
        this.staffList = staffList;
        this.cnn = new ManLabConnection();
        this.main = main;
        this.vi = new ValidateInput();
        this.fDate = new SimpleDateFormatter();
        optionMessage = new OptionMessage(this);
        this.query = new ArrayList<String>();
    }

    public void viewStaffDetail(StaffDetail staff) {
        ViewStaffForm vsf = new ViewStaffForm(main, true);
        vsf.setLblAddressDtl(staff.getStaffAddress());
        vsf.setLblDOBDtl(fDate.convertDateToString(staff.getStaffDOB()));
        vsf.setLblDOJDtl(fDate.convertDateToString(staff.getStaffDOJ()));
        vsf.setLblEduQualDtl(staff.getStaffEduQual());
        vsf.setLblGenderDtl(staff.isStaffGender());
        vsf.setLblStaffNameDtl(staff.toString());
        vsf.setLblUsernameDtl(staff.getStaffUsername());
        vsf.setLblTechSkillDtl(staff.getStaffTechSkill());
        vsf.setLblPhoneDtl(staff.getStaffPhone());
        vsf.setVisible(true);
    }

    public boolean updateStaff(StaffDetail sd) {

        boolean cn=cnn.createConnection();

        String gender = "";
        if (sd.isStaffGender()) {
            gender = "1";
        } else {
            gender = "0";
        }


        qry = "update StaffDetail set staffFName='" + vi.convertSpecialCharacters(sd.getStaffFName()) + "',staffLName='" + vi.convertSpecialCharacters(sd.getStaffLName()) + "',staffDOJ='" + fDate.convertDateToString(sd.getStaffDOJ()) + "',staffAddress='" + sd.getStaffAddress() + "',staffPhone='" + vi.convertSpecialCharacters(sd.getStaffPhone()) + "',staffGender=" + gender + ",staffDOB='" + fDate.convertDateToString(sd.getStaffDOB()) + "',staffEduQual='" + vi.convertSpecialCharacters(sd.getStaffEduQual()) + "',staffTechSkill='" + vi.convertSpecialCharacters(sd.getStaffTechSkill()) + "',staffPassword='" + sd.getStaffPassword() + "' where staffID=" + sd.getStaffID();
        boolean pass = cnn.executeQuery(qry);
        if (pass) {
          //  removeStaff(sd.getStaffID());
         //   staffList.add(sd);
            resetGUI();
            optionMessage.showMessage("Staff has been updated!");
            return true;
        } else {
            optionMessage.showErrorMessage("Staff has not been updated! Try again later!");
            return false;
        }
    }

    private void resetGUI() {
        main.refreshReportTableView();
    }

    private void removeStaff(int staffID) {
        for (StaffDetail staffDetail : staffList) {
            if (staffDetail.getStaffID() == staffID) {
                staffList.remove(staffDetail);
                break;
            }
        }
    }

     public void deleteStaff(StaffDetail staffDetail){
        int stID = staffDetail.getStaffID();
        boolean cn=cnn.createConnection();
        String qry1 = "delete from Report where staffID="+stID+"";
        query.add(qry1);
        String qry2 = "delete from StaffDetail where staffID="+stID+"";
        query.add(qry2);
        for (String s : query) {
            cnn.executeQuery(s);
        }
        cnn.closeConnection();
        removeStaff(stID);
          resetGUI();
        optionMessage.showMessage("Deleted Succesfully!!!");
        this.main.refreshReportTableView();
    }

    public boolean addNewStaff(StaffDetail sd) {
        try {
           boolean cn= cnn.createConnection();
            String gender = "";
            if (sd.isStaffGender()) {
                gender = "1";
            } else {
                gender = "0";
            }
            qry = "insert into StaffDetail values('" + vi.convertSpecialCharacters(sd.getStaffFName()) + "','" + vi.convertSpecialCharacters(sd.getStaffLName()) + "','" + fDate.convertDateToString(sd.getStaffDOJ()) + "','" + vi.convertSpecialCharacters(sd.getStaffAddress()) + "','" + sd.getStaffPhone() + "'," + gender + ",'" + fDate.convertDateToString(sd.getStaffDOB()) + "','" + vi.convertSpecialCharacters(sd.getStaffEduQual()) + "','" + vi.convertSpecialCharacters(sd.getStaffTechSkill()) + "','" + sd.getStaffPassword() + "','')";
            boolean pass = cnn.executeQuery(qry);
            qry = "select top 1 * from StaffDetail order by staffID desc";
            rs = cnn.createResultSet(qry);
            int dID = 0;
            while (rs.next()) {
                dID = rs.getInt("staffID");
            }
            String username = generateUsername(sd.getStaffFName(), sd.getStaffLName(), dID);
            qry = "update StaffDetail set staffUserName='" + username + "' where staffID=" + dID;
            boolean pass1 = cnn.executeQuery(qry);
            if (pass1) {
                sd.setStaffUsername(username);
                main.setLoginPass(true);
//                main.setRegister(false);
            }
            optionMessage.showMessage("Your username is: " + username);
            if (pass) {
                staffList.add(sd);
                return true;
            } else {
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(StaffManage.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    private String generateUsername(String fName, String lName, int id) {
        String[] lNameArray;
        String username = fName;
        lNameArray = lName.split(" ");
        for (int i = 0; i < lNameArray.length; i++) {
            username = username + lNameArray[i].substring(0, 1);
        }
        return (username + "_" + id).toLowerCase();
    }
}
